package com.yiren.dbaa.module.practice_set.line.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.line.contract.LineReleaseContract;
import com.yiren.dbaa.module.practice_set.line.presenter.LineReleasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/16 16:39
 * @description: 线路发布
 */
public class LineReleaseActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_site_name)
    TextView tvSiteName;
    @BindView(R.id.tv_line_name)
    TextView tvLineName;
    @BindView(R.id.tv_line_type)
    TextView tvLineType;
    @BindView(R.id.tv_update_detail)
    TextView tvUpdateDetail;
    @BindView(R.id.ll_updatedetail_tv)
    LinearLayout llUpdatedetailTv;
    @BindView(R.id.et_update_detail)
    EditText etUpdateDetail;
    @BindView(R.id.ll_updatedetail_et)
    LinearLayout llUpdatedetailEt;
    @BindView(R.id.sp_update_way)
    Spinner spUpdateWay;
    @BindView(R.id.ll_update_way)
    LinearLayout llUpdateWay;
    @BindView(R.id.sp_update_user)
    Spinner spUpdateUser;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.ll_update_user)
    LinearLayout llUpdateUser;
    @BindView(R.id.ll_site_rule)
    LinearLayout llSiteRule;

    private LineInfoEntity line;
    private LineReleaseContract.Presenter releasePresenter;
    private UserParamConfigBean userParamConfig;
    private int updateMode = 1;//更新方式 1:提示更新 2:强制更新

    private List<String> updateModes = new ArrayList<>();//更新方式
    private List<String> updateUsers = new ArrayList<>();//更新用户

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_line);
        ButterKnife.bind(this);
        tvTitle.setText("线路数据发布");
        line = (LineInfoEntity) getIntent().getSerializableExtra("line");
        init();
    }

    private void init() {
        releasePresenter = new LineReleasePresenter(this);
        userParamConfig = LoadConfigApi.loadUserParamConfig(this);
        if (line != null) {
            tvSiteName.setText(userParamConfig.getSite().getSiteName());
            tvLineName.setText(line.getName());
            if (line.getUseRange() == 1) {
                tvUsingRangeBt.setVisibility(View.VISIBLE);
            } else {
                tvUsingRangeBt.setVisibility(View.GONE);
            }
            tvLineType.setText(line.getLineType() == 1 ? "考场路段" : "训练路段");
            if (line.getLineVer() == 0) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llSiteRule.getLayoutParams();
                lp.topMargin = 80;
                llSiteRule.setLayoutParams(lp);
                tvUpdateDetail.setText("首次发布");
                llUpdateUser.setVisibility(View.GONE);
                llUpdateWay.setVisibility(View.GONE);
                llUpdatedetailTv.setVisibility(View.VISIBLE);
                llUpdatedetailEt.setVisibility(View.GONE);
            } else {
                llUpdateUser.setVisibility(View.VISIBLE);
                llUpdateWay.setVisibility(View.VISIBLE);
                llUpdatedetailTv.setVisibility(View.GONE);
                llUpdatedetailEt.setVisibility(View.VISIBLE);
            }
        }
        //数据源
        updateModes.add("提示更新");
        updateModes.add("强制更新");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateModes);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateWay.setAdapter(adapter);
        spUpdateWay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                updateMode = position + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        updateUsers.add("个别用户");
        updateUsers.add("全部用户");
        adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateUsers);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateUser.setAdapter(adapter);
        if (line.getUseRange() == 2) {
            spUpdateUser.setSelection(1);
        } else {
            spUpdateUser.setSelection(0);
        }
        spUpdateUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                line.setUseRange(position + 1);
                if (position == 0) {
                    tvUsingRangeBt.setVisibility(View.VISIBLE);
                } else {
                    tvUsingRangeBt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_release_line})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_release_line:
                releasePresenter.releaseLine(line, userParamConfig.getUserInfo().getId().toString(), etUpdateDetail.getText().toString(), updateMode);
                break;
        }
    }

}